
package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/10/13.
 * 七乐彩 胆拖
 */
public class QLCBankerFragment extends MostLotteryFragment {


    private static final int MAX_NUMBER_DANMA = 30;
    private static final int MAX_NUMBER_TUOMA = 30;

    private final Bet bet = new Bet(EnumCode.gameId.QLC);

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupBalls("%02d", 1, MAX_NUMBER_DANMA, MAX_NUMBER_TUOMA);
        bet.setPlayType(EnumCode.PlayType.QLC_DT);
    }

    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        bet.getReds().clear();
        bet.getTuos().clear();
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        parent.findViewById(R.id.random_mark).setVisibility(View.GONE);
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        if (groupIndex == 0) {
            title.setText("胆码");
            tip.setTextColor(red);
            tip.setText("至少选择1个,最多6个");
        } else {
            title.setText("拖码");
            tip.setTextColor(red);
            tip.setText("至少选择2个");
        }
    }


    @Override
    protected int onSetGroupColor(int groupIndex) {
        return red;
    }

    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (index == 0) {//胆码区 最多6个
            if (checked) {
                if (bet.getReds().size() == 6) {
                    Toast.makeText(getContext(), "最多选择6个胆码", Toast.LENGTH_SHORT).show();
                    return false;
                }
                bet.getReds().add(ballText);
            } else {
                bet.getReds().remove(ballText);
            }
            if (checked && parent.isItemChecked(getAdapter().transToRawPosition(position, 1)))//胆码拖码互斥
                parent.setItemChecked(getAdapter().transToRawPosition(position, 1), false);
        } else {//拖码区

            if (checked) {
                if (bet.getReds().size() + bet.getTuos().size() == 21) {
                    Toast.makeText(getContext(), "拖码与胆码之和不能大于21个球", Toast.LENGTH_SHORT).show();
                    return false;
                }
                bet.getTuos().add(ballText);
            } else {
                bet.getTuos().remove(ballText);
            }
            if (checked && parent.isItemChecked(getAdapter().transToRawPosition(position, 0)))//胆码拖码互斥
                parent.setItemChecked(getAdapter().transToRawPosition(position, 0), false);
        }
        if (bet.getReds().size() + bet.getTuos().size() >= 8 && bet.getReds().size() >= 1 && bet.getTuos().size() >= 2) {
            bet.setWagerCount((int) (MathUtils.recursive(bet.getTuos().size(), 7 - bet.getReds().size()) / MathUtils.recursive(7 - bet.getReds().size())));//胆码差几位则从拖码里选择
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
        return true;
    }

}
